package com.ruoyi.project.system.dispatch.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.dispatch.domain.VeDispatch;
import com.ruoyi.project.system.dispatch.service.IVeDispatchService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-01-29
 */
@Controller
@RequestMapping("/system/dispatch")
public class VeDispatchController extends BaseController {
    private String prefix = "system/dispatch";

    @Autowired
    private IVeDispatchService veDispatchService;

    @RequiresPermissions("system:dispatch:view")
    @GetMapping()
    public String dispatch() {
        return prefix + "/dispatch";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:dispatch:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VeDispatch veDispatch) {
        startPage();
        List<VeDispatch> list = veDispatchService.selectVeDispatchList(veDispatch);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:dispatch:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VeDispatch veDispatch) {
        List<VeDispatch> list = veDispatchService.selectVeDispatchList(veDispatch);
        ExcelUtil<VeDispatch> util = new ExcelUtil<VeDispatch>(VeDispatch.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:dispatch:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VeDispatch veDispatch) {
        return toAjax(veDispatchService.insertVeDispatch(veDispatch));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:dispatch:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        VeDispatch veDispatch = veDispatchService.selectVeDispatchById(id);
        mmap.put("veDispatch", veDispatch);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:dispatch:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VeDispatch veDispatch) {
        return toAjax(veDispatchService.updateVeDispatch(veDispatch));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:dispatch:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(veDispatchService.deleteVeDispatchByIds(ids));
    }


    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:dispatch:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(Long id) {
//        VeDispatch veDispatch=veDispatchService.selectVeDispatchById(id);
//        String processDefikey="";
//        Map<String, Object> variables = new HashMap<String,Object>();
//        variables.put("inputUser1", getUserId());
//        //variables.put("inputUser2", 3);
//        ProcessEngine processEngine= ProcessEngines.getDefaultProcessEngine();
//        ProcessInstance pi = processEngine.getRuntimeService()
//                .startProcessInstanceByKey(processDefikey,variables);
//        veDispatch.setFlowInstantId(Long.parseLong(pi.getProcessDefinitionId()));
        return toAjax(veDispatchService.submit(id, getUserId()));
    }

    /**
     * 未完成【请填写功能名称】
     */
    @RequiresPermissions("system:dispatch:noauth")
    @GetMapping("/dispatchnoauth")
    public String dispatchnoauth() {
        return prefix + "/dispatchnoauth";
    }

    /**
     * 查询
     * @param veDispatch
     * @return
     */
    @RequiresPermissions("system:dispatch:listnoauth")
    @PostMapping("/listnoauth")
    @ResponseBody
    public TableDataInfo listnoauth(VeDispatch veDispatch)
    {
        startPage();
        List<VeDispatch> list = veDispatchService.noFinishTask(getUserId(),"ApplyProcess");
        return getDataTable(list);
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:dispatch:editnoauth")
    @GetMapping("/editnoauth/{id}")
    public String editnoauth(@PathVariable("id") Long id, ModelMap mmap) {
        VeDispatch veDispatch = veDispatchService.selectVeDispatchById(id);
        mmap.put("veDispatch", veDispatch);
        mmap.put("taskCommentVoList",veDispatchService.getTaskCommentList(veDispatch.getFlowInstantId()));
        return prefix + "/editnoauth";
    }

    @RequiresPermissions("system:dispatch:editnoauth")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/editnoauthdata")
    @ResponseBody
    public AjaxResult editSaveNoAuthData(VeDispatch veDispatch) {
        veDispatchService.completeTask(veDispatch.getFlowInstantId(),veDispatch.getAgree(),getUserId(),veDispatch.getComment(),veDispatch.getId());
        return toAjax(200);
    }


    /**
     * 查询
     * @param
     * @return
     */
    @RequiresPermissions("system:dispatch:finish")
    @GetMapping("/dispatchfinish")
    public String dispatchfinish() {
        return prefix + "/dispatchfinish";
    }

    /**
     * 查询
     * @param veDispatch
     * @return
     */
    @RequiresPermissions("system:dispatch:listfinish")
    @PostMapping("/listfinish")
    @ResponseBody
    public TableDataInfo listfinish(VeDispatch veDispatch)
    {
        startPage();
        List<VeDispatch> list = veDispatchService.finishTask(getUserId(),"ApplyProcess");
        return getDataTable(list);
    }


    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:dispatch:editfinish")
    @GetMapping("/editfinish/{id}")
    public String editfinish(@PathVariable("id") Long id, ModelMap mmap) {
        VeDispatch veDispatch = veDispatchService.selectVeDispatchById(id);
        mmap.put("veDispatch", veDispatch);
        mmap.put("taskCommentVoList",veDispatchService.getTaskCommentList(veDispatch.getFlowInstantId()));
        return prefix + "/editfinish";
    }
}